<template>
  <div id="addcate_app">
    <el-dialog
      :title="cate_form.id ? '修改商品分类' : '添加商品分类'"
      :visible.sync="isShow.flag"
      width="50%"
    >
      <el-form
        :model="cate_form"
        ref="cate_form"
        label-width="100px"
        class="demo-ruleForm"
        :rules="rules"
      >
        <el-form-item label="上级分类" prop="id">
          <el-select v-model="cate_form.pid" placeholder="请选择商品分类">
            <el-option :label="'一级分类'" :value="0"></el-option>
            <el-option
              v-for="cate in cateList"
              :key="cate.id"
              :label="cate.catename"
              :value="cate.id"
              >{{ cate.catename }}</el-option
            >
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" prop="catename">
          <el-input v-model="cate_form.catename"></el-input>
        </el-form-item>

        <el-form-item label="分类图片" v-if="cate_form.pid !== 0">
          <el-upload class="avatar-uploader" action="#" :show-file-list="true" :on-change="change_img">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="分类状态">
          <el-radio-group v-model="cate_form.status" prop="status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isShow.flag = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  methods: {
    submit() {
      var url = "";
      if(this.cate_form.id){
        url="/cateedit"
      }else{
        url = "/cateadd"
      }
      //构建formData实例
      var formData = new FormData();
      //提交请求
      for (let obj in this.cate_form) {
        formData.append(obj, this.cate_form[obj]);
      }

      this.$axios.post(url, formData).then((res) => {
        if (res.code === 200) {
          //重置表单
          this.cate_form = {
            pid: "",
            catename: "",
            status: "",
            img: "",
          };
          //关闭模态框
          this.isShow.flag = false;
          //刷新页面
          this.$emit("reload");
          //弹出提示
          this.$message({
            type: "success",
            message: "添加成功",
          });
        }
      });
    },
    setData(obj) {
      this.cate_form = { ...obj };
      this.imageUrl = this.$url + this.cate_form.img;
    },
    change_img(file) {
      //获取图片对象 upload组件自带 on-change
      this.cate_form.img = file.raw;
    },
  },
  props: ["isShow", "cateList"],
  data() {
    return {
      cate_form: {
        pid: "",
        catename: "",
        status: 1,
        img: "",
      },
      imageUrl: "",
      rules: {
        id: { required: true, message: "商品分类不可以为空", trigger: "blur" },
        catename: {
          required: true,
          message: "商品分类名称不可以为空",
          trigger: "blur",
        },
        status: {
          required: true,
          message: "商品分类状态不可以为空",
          trigger: "blur",
        },
      },
    };
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>